// pages/myorders/myorders.js
var common = require("../../utils/Conmon.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    wulimodel: true,
    active1: true,
    active2: false,
    active3: false,
    active4: false,
    active5: false,
    active6: false,
    PageIndex: 1,
    PageSize: 3,
    OrderStatus: 'All',
    OrderList: [],
    IsLastPage: false,
    nodata: true,
  },
  wulimodel: function () {
    this.setData({
      wulimodel: false,
    })
  },
  wuliclose: function () {
    this.setData({
      wulimodel: true,
    })
  },
  tabchang1: function () {
    this.setData({
      active1: true,
      active2: false,
      active3: false,
      active4: false,
      active5: false,
      active6: false,
      PageIndex: 1,
      OrderStatus: 'All',
    })
    LoadOrderList(this, this.data.OrderStatus, 1);
  },
  tabchang2: function () {
    this.setData({
      active1: false,
      active2: true,
      active3: false,
      active4: false,
      active5: false,
      active6: false,
      PageIndex: 1,
      OrderStatus: 'WaitPay',
    })
    LoadOrderList(this, this.data.OrderStatus, 1);
  },
  tabchang3: function () {
    this.setData({
      active1: false,
      active2: false,
      active3: true,
      active4: false,
      active5: false,
      active6: false,
      PageIndex: 1,
      OrderStatus: 'WaitDeliver',
    })
    LoadOrderList(this, this.data.OrderStatus, 1);
  },
  tabchang4: function () {
    this.setData({
      active1: false,
      active2: false,
      active3: false,
      active4: true,
      active5: false,
      active6: false,
      PageIndex: 1,
      OrderStatus: 'WaitReceipt',
    })
    LoadOrderList(this, this.data.OrderStatus, 1);
  },
  tabchang5: function () {
    this.setData({
      active1: false,
      active2: false,
      active3: false,
      active4: false,
      active5: true,
      active6: false,
      PageIndex: 1,
      OrderStatus: 'WaiteValuate',
    })
    LoadOrderList(this, this.data.OrderStatus, 1);
  },
  tabchang6: function () {
    this.setData({
      active1: false,
      active2: false,
      active3: false,
      active4: false,
      active5: false,
      active6: true,
      PageIndex: 1,
      OrderStatus: 'Refund',
    })
    LoadOrderList(this, this.data.OrderStatus, 1);
  },
  linkTo: function () {

    wx.switchTab({
      url: '/pages/customIndex/index'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    common.pageInit(function () {
    });
    var res = wx.getStorageSync('UserInfo');
    if (res == "") {
      wx.redirectTo({
        url: '/pages/login/login',
      })
      return false;
    }
    var that = this;
    var Status = that.data.OrderStatus;
    var orderState = wx.getStorageSync('OrderStatusUserInfo');

    if (orderState == '0') {
      that.setData({
        active1: true,
        active2: false,
        active3: false,
        active4: false,
        active5: false,
        active6: false,
        PageIndex: 1,
        OrderStatus: 'All'
      })
      Status = 'All';
    }
    else if (orderState == '1') {
      that.setData({
        active1: false,
        active2: true,
        active3: false,
        active4: false,
        active5: false,
        active6: false,
        PageIndex: 1,
        OrderStatus: 'WaitPay'
      })
      Status = 'WaitPay';
    }
    else if (orderState == '2') {
      that.setData({
        active1: false,
        active2: false,
        active3: true,
        active4: false,
        active5: false,
        active6: false,
        PageIndex: 1,
        OrderStatus: 'WaitDeliver'
      })
      Status = 'WaitDeliver';
    }
    else if (orderState == '3') {
      that.setData({
        active1: false,
        active2: false,
        active3: false,
        active4: true,
        active5: false,
        active6: false,
        PageIndex: 1,
        OrderStatus: 'WaitReceipt'
      })
      Status = 'WaitReceipt';
    }
    else if (orderState == '4') {
      that.setData({
        active1: false,
        active2: false,
        active3: false,
        active4: false,
        active5: true,
        active6: false,
        PageIndex: 1,
        OrderStatus: 'WaiteValuate'
      })
      Status = 'WaiteValuate';
    }
    else if (orderState == '5') {
      that.setData({
        active1: false,
        active2: false,
        active3: false,
        active4: false,
        active5: false,
        active6: true,
        PageIndex: 1,
        OrderStatus: 'Refund'
      })
      Status = 'Refund';
    }
    LoadOrderList(that, Status, 1);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    //wx.removeStorageSync('OrderStatusUserInfo');
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    LoadOrderList(this, this.data.OrderStatus, 1);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    if (this.data.IsLastPage && this.data.PageIndex != 1) {
      wx.showToast({
        title: '已到底',
        duration: 2000
      })
      return false;
    }
    var index = this.data.PageIndex;
    index = index + 1;
    this.setData({
      PageIndex: index,
    })
    var that = this;
    LoadOrderList(that, that.data.OrderStatus, index);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 申请退款
   */
  onApplyRefund: function (event) {

    wx.navigateTo({
      url: '/pages/returngoods/returngoods?OrderID=' + event.currentTarget.id + "&shopID=" + event.currentTarget.dataset.shopid
    })
  },

  /**
   * 取消申请退款
   */
  onCancelRefund: function (event) {
    var globalData = getApp().globalData;
    var custId = globalData.custID;
    var url = globalData.apiurl + '/order/api/OrderInfo/CancelRefundOrder';
    var returnNo = event.target.id;
    var that = this;
    wx.showModal({
      title: '取消申请退款',
      content: '确定取消申请退款吗？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: url,
            data: { "custId": custId, "returnNo": returnNo, "unionStoreID": event.currentTarget.dataset.shopid },
            method: 'GET',
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              wx.showToast({
                title: '取消申请退款成功',
                duration: 2000
              })
              LoadOrderList(that, that.data.OrderStatus, 1);
            },
            fail: function () {
              wx.showToast({
                title: '取消申请退款失败',
                duration: 2000
              })
            },
            complete: function () {
              //complete
            }
          })
        }
      }
    })
  },

  /**
   * 取消订单
   */
  onCancelOrder: function (event) {
    var globalData = getApp().globalData;
    var custId = globalData.custID;
    var res = wx.getStorageSync('UserInfo');
    var userId = res.UserID;
    var url = globalData.apiurl + '/order/api/OrderInfo/CancelOrder';
    var OID = event.target.id;
    var unionStoreID = event.currentTarget.dataset.shopid;
    var that = this;
    wx.showModal({
      title: '取消订单',
      content: '确定取消该订单吗？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: url,
            data: { "custId": custId, "userId": userId, "orderId": OID, "reason": '个人取消', "unionStoreID": unionStoreID },
            method: 'GET',
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              wx.showToast({
                title: '取消订单成功',
                duration: 2000
              })
              LoadOrderList(that, that.data.OrderStatus, 1);
            },
            fail: function () {
              wx.showToast({
                title: '取消订单失败',
                duration: 2000
              })
            },
            complete: function () {
              //complete
            }
          })
        }
      }
    })
  },

  /**
   * 删除订单
   */
  onDeleteOrder: function (event) {
    var globalData = getApp().globalData;
    var custId = globalData.custID;
    var url = globalData.apiurl + '/order/api/OrderInfo/DeleteOrder';
    var OID = event.target.id;
    var unionStoreID = event.currentTarget.dataset.shopid;
    var that = this;
    wx.showModal({
      title: '删除订单',
      content: '确定删除该订单吗？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: url,
            data: { "custId": custId, "orderId": OID, "unionStoreID": unionStoreID },
            method: 'GET',
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              wx.showToast({
                title: '删除订单成功',
                duration: 2000
              })
              LoadOrderList(that, that.data.OrderStatus, 1);
            },
            fail: function () {
              wx.showToast({
                title: '删除订单失败',
                duration: 2000
              })
            },
            complete: function () {
              //complete
            }
          })
        }
      }
    })
  },

  /**
   * 确认收货
   */
  onReceiveOrder: function (event) {
    var globalData = getApp().globalData;
    var custId = globalData.custID;
    var url = globalData.apiurl + '/order/api/OrderInfo/ReceiveOrder';
    var OID = event.target.id;
    var that = this;
    var unionStoreID = event.currentTarget.dataset.shopid;
    wx.showModal({
      title: '确认收货',
      content: '确定确认收货吗？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: url,
            data: { "custId": custId, "orderId": OID, "unionStoreID": unionStoreID },
            method: 'GET',
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              wx.showToast({
                title: '确认收货成功',
                duration: 2000
              })
              LoadOrderList(that, that.data.OrderStatus, 1);
            },
            fail: function () {
              wx.showToast({
                title: '确认收货失败',
                duration: 2000
              })
            },
            complete: function () {
              //complete
            }
          })
        }
      }
    })
  },

  /**
   * 跳转订单详情
   */
  onRedirectToOrderInfo: function (event) {

    wx.navigateTo({
      url: '/pages/orderDetails/orderDetails?OrderID=' + event.currentTarget.id + "&shopID=" + event.currentTarget.dataset.shopid
    })
  },

  /**
   * 付款
   */
  onGoPay: function (event) {

    wx.navigateTo({
      url: '/pages/unpaid/unpaid?OrderID=' + event.currentTarget.id + "&unionShopID=" + event.currentTarget.dataset.shopid,
      complete: function (res) {
        //设置返回的时候是代付款
        //添加缓存，用缓存来接收参数
        wx.setStorageSync("OrderStatusUserInfo", 1)
      }
    })
  },

  /**
   * 立即评价
   */
  onComment: function (event) {
    wx.navigateTo({
      url: '/pages/addComment/addComment?OrderID=' + event.currentTarget.id + "&shopID=" + event.currentTarget.dataset.shopid
    })
  }

})

//获取订单列表
function LoadOrderList(obj, OrderStatus, PageIndex) {
  wx.showLoading({
    title: '加载中...',
    mask: true
  });

  var globalData = getApp().globalData;
  var custId = globalData.custID;
  var res = wx.getStorageSync('UserInfo');
  var userId = res.UserID;
  var pageSize = obj.data.PageSize;
  var url = globalData.apiurl + '/order/api/OrderInfo/GetMyOrderList';

  wx.request({
    url: url,
    data: { "custId": custId, "userId": userId, "orderStatus": OrderStatus, "pageIndex": PageIndex, "pageSize": pageSize },
    method: 'GET',
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
    
     
    
        wx.hideLoading();
      var list = res.data.content;
      if (PageIndex > 1) {
        var olist = obj.data.OrderList;
        for (var i = 0, len = list.length; i < len; i++) {
          olist.push(list[i]);
        }
        obj.setData({
          OrderList: olist,
          IsLastPage: false
        });
        if (list.length < pageSize) {
          obj.setData({
            OrderList: olist,
            IsLastPage: true
          })
        }
      } else {
        if (list.length == 0) {
          obj.setData({
          
            OrderList: [],
            nodata: false
          });
        } else {
          obj.setData({
            OrderList: list,
            IsLastPage: false
           
          });
        }
      }

     
    },
    fail: function () {
      wx.hideLoading();
      wx.showToast({
        title: '获取列表失败',
        duration: 2000
      })
    },
    complete: function () {
      //complete
    }
  })
}
